1 //============================================================================
2 // A virtual memory allocation wrapper. This is used to make sure that we
3 // don't leak any of this memory.
5 // This allocator assumes that the memory it was allocated out of is zero
7 //============================================================================
14 // Constructors/Destructors.
21 // Helpers to make sure that we don't try to allocate bad sizes.
25 static size_t CbPageSize()
30 // Adjust the size of memory to reserve.
32 size_t CbSizeReserve(size_t cbSize
)
34 // Make sure we always alloc something.
35 cbSize
= cbSize
? cbSize
: 1;
37 return(cbSize
+ m_cbAllocationGranularity
- 1)
38 & ~(m_cbAllocationGranularity
- 1);
41 // Adjust the size of memory to allocate.
43 size_t CbSizeAlloc(size_t cbSize
)
45 // Make sure we always alloc something.
46 cbSize
= cbSize
? cbSize
: 1;
47 return(cbSize
+ m_cbSizePage
- 1) & ~(m_cbSizePage
- 1);
50 // Minumum allocation granularity.
52 size_t CbAllocationGranularity()
54 return m_cbAllocationGranularity
;
58 // Address space manipulation methods.
61 // Reserve some address space to allocate in.
62 void * ReserveAddressSpace(size_t cbSize
);
64 // Unreserve the space.
65 void FreeAddressSpace(void * pv
);
67 // Commit memory inside of an address space.
72 // Uncommit the space.
77 // Change the read-onlyness of a page of memory.
84 // Reset the passcount on a chunk of memory.
85 void DebMarkMemory(void * pv
);
87 void DebMarkMemory(void * pv
)
94 // The following members are used to determine the size of
95 // the pages we allocate.
97 static size_t m_cbSizePage
;
98 static size_t m_cbAllocationGranularity
;
102 unsigned m_passCount
;
104 // This structure holds the information about the allocated
109 AddressSpace
*m_pNextSpace
; // Next field in the linked list.
111 void *m_pvAddressSpace
; // The address of the beginning of the space
112 size_t m_cbAddressSpace
; // The size of the reserved space
114 size_t m_iPassCount
; // Which allocation is this?
117 // List of allocated virutal spaces.
118 AddressSpace
*m_pAllocatedAddressSpaces
;
125 /* this is an internal class that shouldn't be used directly.
126 Use the TEMPBUF macro instead
135 if (size
<= stacksize
)
137 m_pcBuffer
= stackbuffer
;
138 m_where_allocated
= LOCAL
;
142 m_pcBuffer
= CoTaskMemAlloc(size
);
143 m_where_allocated
= HEAP
;
149 if (m_where_allocated
== HEAP
) {
150 CoTaskMemFree(m_pcBuffer
);
160 enum alloc_kind
{LOCAL
, HEAP
};
161 alloc_kind m_where_allocated
;
165 /* Macro to declare and efficiently manage a temporary buffer.
166 * Var is the variable name
168 * size is the actual (known at runtime) size of the buffer
170 * TEMPBUF(tempchars, char*, 2*len(initchars));
172 * NOTE: you must check the pointer to the buffer for NULL in case
173 * the allocator wasn't able to allocate the memory.
175 #pragma warning(disable:6304)//,"It is OK for address-of to be a NOP for temp buffers.")
177 #define TEMPBUF(var,type,size) \
178 char __TempBuf##var[120]; \
179 TempBuffer __TempBuffer##var(120,(size),(char*)&__TempBuf##var); \
180 type var = (type)__TempBuffer##var.GetBuffer();
182 #pragma warning(default:6304)//,"It is OK for address-of to be a NOP for temp buffers.")